package org.tuestudent.grouppa;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class ChangePinPopup extends DialogFragment implements OnClickListener {

    /**
     * creates a popup for pin changing
     * @param currentPin
     * @param listener listener with method to call when pin is changed
     */
    public ChangePinPopup(String currentPin, OnPinChangeListener listener)
    {
        super();
        if(listener == null)
            throw new IllegalArgumentException("changePinPopup constructor: listener is null");
        this.currentPin = currentPin;
        this.listener = listener;
    }
    /**
     * old pin
     */
    final String currentPin;
    /**
     * called when pin is succesfully changed
     */
    final OnPinChangeListener listener;
    View popupView;
    EditText oldPasswordTextField;
    EditText newPasswordTextField;
    EditText confirmPasswordTextField;
    ImageButton cancelButton;
    Button changePinSaveButton;
    
    /**
     * called when pin is changed
     */
    public interface OnPinChangeListener
    {
        /**
         * should store the new pin and close the popup
         * @param newPin
         * @param popup
         */
        void pinChange(String newPin, ChangePinPopup popup);
    }
    
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        ContextThemeWrapper cw =
                new ContextThemeWrapper(getActivity(), R.style.AlertDialogTheme);
        AlertDialog.Builder builder = new AlertDialog.Builder(cw);
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        popupView = inflater.inflate(R.layout.popup_change_pin, null);
        builder.setView(popupView);
        
        oldPasswordTextField = (EditText) popupView.findViewById(R.id.oldPassword);
        newPasswordTextField = (EditText) popupView.findViewById(R.id.newPassword);
        confirmPasswordTextField = (EditText) popupView.findViewById(R.id.confirmPassword);
        cancelButton = (ImageButton) popupView.findViewById(R.id.cancelButton);
        changePinSaveButton = (Button) popupView.findViewById(R.id.changePinSave);
        
        cancelButton.setOnClickListener(this);
        changePinSaveButton.setOnClickListener(this);
        return builder.create();
    }
    @Override
    public void onClick(View v) {
        if(v==cancelButton)
        {
            dismiss();
            return;
        }
        if(v==changePinSaveButton)
        {
            String oldPin = oldPasswordTextField.getText().toString();
            String newPin = newPasswordTextField.getText().toString();
            String confirmPin = confirmPasswordTextField.getText().toString();
            
            oldPasswordTextField.setError(null);
            newPasswordTextField.setError(null);
            confirmPasswordTextField.setError(null);
            
            if(!oldPin.equals(currentPin))
            {
                oldPasswordTextField.setError("Wrong Pin.");
                return;
            }
            if(newPin.length() != 4)
            {
                newPasswordTextField.setError("New Pin should have exactly 4 digits.");
                return;
            }
            if(!confirmPin.equals(newPin))
            {
                confirmPasswordTextField.setError("Pins don't match");
                return;
            }
            
            listener.pinChange(newPin, this);
        }
    }
}
